package util;

public class Vector3D {

	private float x, y, z;

	public Vector3D(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.normalize();
	}

	public Vector3D(Point3D start, Point3D end) {
		this.x = end.getX() - start.getX();
		this.y = end.getY() - start.getY();
		this.z = end.getZ() - start.getZ();
		this.normalize();
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
		this.normalize();
	}

	public float getY() {
		return y;
	}

	public void setY(float y) {
		this.y = y;
		this.normalize();
	}

	public float getZ() {
		return z;
	}

	public void setZ(float z) {
		this.z = z;
		this.normalize();
	}

	public String toString() {
		return "Vector3D[" + x + "," + y + "," + z + "]";
	}

	public void normalize() {
		float length = this.getLength();
		x = x / length;
		y = y / length;
		z = z / length;
	}

	private float getLength() {
		return (float) Math.sqrt(Math.pow(x, 2.0f) + Math.pow(y, 2.0f)
				+ Math.pow(z, 2.0f));
	}

	public float dotProduct(float otherX, float otherY, float otherZ) {
		return (this.x * otherX) + (this.y * otherY) + (this.z * otherZ);
	}

	public float dotProduct(Vector3D other) {
		return (this.x * other.getX()) + (this.y * other.getY())
				+ (this.z * other.getZ());
	}

	public Vector3D mulScalar(float scalar) {
		return new Vector3D(this.x * scalar, this.y * scalar, this.z * scalar);
	}

	public Vector3D minus(Vector3D other) {
		return new Vector3D(this.x - other.getX(), this.y - other.getY(),
				this.z - other.getZ());
	}
	
	public Vector3D plus(Vector3D other) {
		return new Vector3D(this.x + other.getX(), this.y + other.getY(),
				this.z + other.getZ());
	}

	public Vector3D crossProduct(Vector3D other) {
		return new Vector3D((this.y * other.getZ()) - (this.z * other.getY()),
				(this.z * other.getX()) - (this.x * other.getZ()),
				(this.x * other.getY()) - (this.y * other.getX()));
	}

}
